#include "LVGLArc.h"

#include <QIcon>

#include "LVGLObject.h"
#include "properties/LVGLPropertyVal2.h"
#include "lvgldesigner.h"

static void _arc_set_angles(lv_obj_t * obj, uint16_t start, uint16_t end)
{
    lv_arc_set_angles(obj, start, end);

    if (LVGLDesigner::transport != NULL) {
        pLvObject  arc;

        arc = LVGLDesigner::findObject(obj);
        if (arc != 0) {
            isd_arc_set_angles(arc, start, end);
        }
    }
}

LVGLArc::LVGLArc()
{
	m_properties << new LVGLPropertyVal2UInt16(0, 360, "Start", lv_arc_get_angle_start,
															 0, 360, "End", lv_arc_get_angle_end,
                                                             "lv_arc_set_angles", _arc_set_angles, "Angles");

	m_editableStyles << LVGL::StyleParts(LVGL::Body | LVGL::Line); // LV_GAUGE_STYLE_MAIN

    setPreview(QPixmap(":/Arc.png"));
}

QString LVGLArc::name() const
{
	return "Arc";
}

QString LVGLArc::className() const
{
	return "lv_arc";
}

LVGLWidget::Type LVGLArc::type() const
{
	return Arc;
}

QIcon LVGLArc::icon() const
{
	return QIcon();
}

lv_obj_t *LVGLArc::newObject(lv_obj_t *parent) const
{
	lv_obj_t *obj = lv_arc_create(parent, nullptr);

    if (LVGLDesigner::transport != NULL) {
        pLvObject  main_ctx;
        pLvObject  btn;

        main_ctx = LVGLDesigner::findObject(parent);
        if (main_ctx != 0) {
            btn = isd_arc_create(main_ctx);
            LVGLDesigner::registerObject(obj, btn);
        }
    }
	return obj;
}

QSize LVGLArc::minimumSize() const
{
	return {75, 75};
}

QStringList LVGLArc::styles() const
{
	return QStringList() << "LV_ARC_STYLE_MAIN";
}

lv_style_t *LVGLArc::style(lv_obj_t *obj, int type) const
{
	return const_cast<lv_style_t*>(lv_arc_get_style(obj, type & 0xff));
}

void LVGLArc::setStyle(lv_obj_t *obj, int type, lv_style_t *style) const
{
	lv_arc_set_style(obj, static_cast<lv_arc_style_t>(type), style);
}

lv_style_t *LVGLArc::defaultStyle(int type) const
{
	if (type == LV_ARC_STYLE_MAIN)
		return &lv_style_plain_color;
	return nullptr;
}
